package com.huicoo.forestmanager;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import com.huicoo.forestmanager.others.Constants;
import com.huicoo.forestmanager.util.SharedPreferenceUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

public class CustomUtils {
    /**
     * 获取用户token
     */
    public static String getToken() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.TOKEN);
    }

    /**
     * 获取用户id
     */
    public static String getUserID() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_ID);
    }


    /**
     * 获取用户name
     */
    public static String getUserName() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_REAL_NAME);
    }

    /**
     * 获取用户account
     */
    public static String getUserAccount() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_ACCOUNT);
    }

    /**
     * 获取用户password
     */
    public static String getUserPassword() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_PASSWORD);
    }

    /**
     * 获取全局url
     */
    public static String getBaseUrl() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.BASE_URL);
    }

    /**
     * 获取全局url的key 即通道码
     */
    public static String getBaseUrlKey() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.BASE_URL_KEY);
    }

    /**
     * 获取用户等级
     */
    public static String getUserLevel() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_LEVEL);
    }

    /**
     * 获取用户类型
     */
    public static String getUserType() {
        return SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_TYPE);
    }

    /**
     * 获取当前app version code
     */
    public static long getAppVersionCode(Context context) {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appVersionCode;
    }

    /**
     * 获取当前app version name
     */
    public static String getAppVersionName(Context context) {
        String appVersionName = "";
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            appVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return appVersionName;
    }

    /**
     * web 拼接参数
     */
    public static String urlAddParams(Context context, String url) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }

        String mUrl = url;

        if (!mUrl.contains("Token=")) {
            if (!mUrl.contains("?")) {
                mUrl += "?";
            } else {
                mUrl += "&";
            }
            mUrl += "Token=" + CustomUtils.getToken();
        }

        if (!mUrl.contains("UserId=")) {
            mUrl += "&UserId=" + CustomUtils.getUserID();
        }

        if (!mUrl.contains("Appid=")) {
            mUrl += "&Appid=" + SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.UNIQUE_ID);
        }

        if (!mUrl.contains("Appversion=")) {
            mUrl += "&Appversion=" + CustomUtils.getAppVersionName(context);
        }

        if (!mUrl.contains("App=")) {
            mUrl += "&App=forestmanager_sj_" + getBaseUrlKey();
        }

        if (!mUrl.contains("UserLevel=")) {
            mUrl += "&UserLevel=" + CustomUtils.getUserLevel();
        }

        if (!mUrl.contains("UserType=")) {
            try {
                mUrl += "&UserType=" + URLEncoder.encode(CustomUtils.getUserType(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return mUrl;
    }

    /**
     * 获取随机码UUID
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }
}
